package backend.service.other;

import backend.model.other.User;
import backend.service.base.DBModelServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends DBModelServiceImpl<User> implements UserService {

    @Override
    public Class<User> getEntityClass() {
        return User.class;
    }

    @Override
    public User findUserByUsernameAndPassword(String username, String password) {
        List<User> users = dbModelDao.findByNamedQueryAndParams("findUserByUsernameAndPassword",
                new String[]{"username", "password"}, new Object[]{username, password});
        assert users.size() == 1;
        if (users.size() == 1) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public User findUserByUsername(String username) {
        List<User> users = dbModelDao.findByNamedQueryAndParams("findUserByUsername",
                new String[]{"username"}, new Object[]{username});
        assert users.size() == 1;
        if (users.size() == 1) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public List<User> findAllActiveUsers() {
        return dbModelDao.findByNamedQuery("findAllActiveUsers");
    }
}
